//
//  LunBoPicture.m
//  UI10HomeWork
//
//  Created by wjn on 15/11/9.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "LunBoPicture.h"

@implementation LunBoPicture

- (void)dealloc {
    [_array release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        _array = [[NSMutableArray alloc] initWithArray:array];
        [self createScollView];
    }
    return self;
}

- (void)createScollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.pagingEnabled = YES;
   
    if (self.array.count) {
        
        [_array insertObject:[self.array lastObject] atIndex:0];
        [_array addObject:self.array[1]];
        
        for (int i = 0; i < self.array.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
            imageView.image = [UIImage imageNamed:self.array[i]];
//            保持图片的比例不变填充满屏幕
            imageView.contentMode = UIViewContentModeScaleAspectFill;
//            超越父视图边界以外不显示
            imageView.clipsToBounds = YES;
            [scrollView addSubview:imageView];
            [imageView release];
            
        }
    }
     scrollView.contentSize = CGSizeMake(self.frame.size.width * _array.count, 0);
    scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    scrollView.delegate = self;
    [self addSubview:scrollView];
    [scrollView release];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    判断是否滑动到第一张
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width * (self.array.count - 2), 0);
    }
    
    if (scrollView.contentOffset.x == self.frame.size.width * (self.array.count - 1)) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    
}




@end
